package businesslogic.teachPlanBl;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.ArrayList;

import dataservice.ResultMessage;
import dataservice.courseDataService.CourseDatabaseService;
import dataservice.teachPlanDataService.TeachPlanDatabaseService;
import po.coursePO.CoursePO;
import po.teachPlanPO.TeachPlanPO;
import vo.teachPlanVO.TeachPlanVO;
import businesslogicservice.teachPlanblservice.TeachPlanblservice;

public class TeachPlan implements TeachPlanblservice {
	private static final String ADDRESS_STRING="localhost";
	
/*
 * 根据学期得到教学计划
 */
	public ArrayList<TeachPlanVO> getPlanBySemester(String semester ,String department) throws Exception{
		ArrayList<TeachPlanVO> teachPlanVOs = new ArrayList<TeachPlanVO>();
		try {
			TeachPlanDatabaseService td = (TeachPlanDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/teachPlanData");
			ArrayList<TeachPlanPO> teachPlanPOs = td.getPlanBySemester(semester,department);
			for (TeachPlanPO t : teachPlanPOs) {
				TeachPlanVO teachPlanVO = null;
				CoursePO c = td.find(t.getCourseID());
				if (c != null) {
					teachPlanVO = new TeachPlanVO(c.getCourseId(),
							c.getCourseName(), c.getCredit(),
							c.getDepartmentID(), c.getAttribute(), semester);
					teachPlanVOs.add(teachPlanVO);
				}
			}
		} catch (RemoteException e) {
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
			throw e;
		} catch (Exception e) {
			e.printStackTrace();	
		}
		return teachPlanVOs;
	}

	/*
	 * 得到本院系未添加入教学计划的课程
	 */
	public ArrayList<TeachPlanVO> getUnselectedCourse(String department) throws Exception{
		ArrayList<TeachPlanVO> teachPlanVOs = new ArrayList<TeachPlanVO>();
		try {
			TeachPlanDatabaseService td = (TeachPlanDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/teachPlanData");
			ArrayList<CoursePO> coursePOs = td.getUnselectedCourse(department);
			for (CoursePO c : coursePOs) {
				TeachPlanVO teachPlanVO = new TeachPlanVO(c.getCourseId(),
						c.getCourseName(), c.getCredit(),
						c.getDepartmentID(), c.getAttribute(), null);
				teachPlanVOs.add(teachPlanVO);
			}
		} catch (RemoteException e) {
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return teachPlanVOs;
	}

	/*
	 * 得到院系教学计划
	 */
	public ArrayList<TeachPlanVO> getPlanByDepartment(String department) throws Exception{
		ArrayList<TeachPlanVO> teachPlanVOs = new ArrayList<TeachPlanVO>();
		try {
			TeachPlanDatabaseService td = (TeachPlanDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/teachPlanData");
			CourseDatabaseService cd = (CourseDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/courseData");
			ArrayList<TeachPlanPO> teachPlanPOs = td.getPlanByDepartment(department);
			for (TeachPlanPO t : teachPlanPOs) {
				TeachPlanVO teachPlanVO = null;
				CoursePO c = cd.find(t.getCourseID());
				if (c != null) {
					teachPlanVO = new TeachPlanVO(c.getCourseId(),
							c.getCourseName(), c.getCredit(),
							c.getDepartmentID(), c.getAttribute(), t.getSemester());
					teachPlanVOs.add(teachPlanVO);
				}
			}
		} catch (RemoteException e) {
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return teachPlanVOs;
	}

	/*
	 * 添加教学计划
	 */
	public ResultMessage addTeachPlan(TeachPlanVO teachPlanVO) throws Exception{
		try {
			TeachPlanDatabaseService td = (TeachPlanDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/teachPlanData");
			TeachPlanPO teachPlanPO=new TeachPlanPO(teachPlanVO.getSemester(),teachPlanVO.getCourseId(), teachPlanVO.getDepartment());
			return (td.insert(teachPlanPO));
		} catch (RemoteException e) {
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultMessage.INSERT_FAILURE;
	}

	/*
	 * 删除教学计划
	 */
	public ResultMessage removeTeachPlan(TeachPlanVO teachPlanVO)throws Exception{
		try {
			TeachPlanDatabaseService td = (TeachPlanDatabaseService) Naming
					.lookup("rmi://"+ADDRESS_STRING+":8888/teachPlanData");
			TeachPlanPO teachPlanPO=new TeachPlanPO(teachPlanVO.getSemester(),teachPlanVO.getCourseId(), teachPlanVO.getDepartment());
			return (td.delete(teachPlanPO));
		} catch (RemoteException e) {
			System.out.println(e.getCause());
			System.out.println(e.getMessage());
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultMessage.DELETE_FAILURE;
	}
}
